package com.lzyyj.gmis.base.webui;

import com.lzyyj.gmis.base.model.ThemeSkin;
import com.lzyyj.gmis.base.model.ThemeStyle;
import com.lzyyj.gmis.base.model.ThemeType;
import com.lzyyj.gmis.base.service.IThemeService;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSessionEvent;
import jakarta.servlet.http.HttpSessionListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Map;

public class ThemeListener implements ServletContextListener, HttpSessionListener {

	private static final Log log = LogFactory.getLog(ThemeListener.class);

	private IThemeService themeService;
	
	private String themeRootKey = ThemeSkin.THEME_ROOT_KEY;

	private String themeTypeKey = ThemeSkin.THEME_TYPE_KEY;

	private String themeStyleKey = ThemeSkin.THEME_STYLE_KEY;

	private String themeMainCssKey = ThemeSkin.THEME_MAIN_CSS_KEY;
	
	private String defaultThemeRoot = "Skin";

	private String defaultThemeType = "SiteWeaver";

	private String defaultThemeStyle = "Default" ;
	
	private ThemeSkin themeSkin;
	
	public IThemeService getThemeService() {
		return themeService;
	}

	public void setThemeService(IThemeService themeService) {
		this.themeService = themeService;
	}

	public String getThemeRootKey() {
		return themeRootKey;
	}

	public void setThemeRootKey(String themeRootKey) {
		this.themeRootKey = themeRootKey;
	}

	public String getThemeTypeKey() {
		return themeTypeKey;
	}

	public void setThemeTypeKey(String themeTypeKey) {
		this.themeTypeKey = themeTypeKey;
	}

	public String getThemeStyleKey() {
		return themeStyleKey;
	}

	public void setThemeStyleKey(String themeStyleKey) {
		this.themeStyleKey = themeStyleKey;
	}

	public String getThemeMainCssKey() {
		return themeMainCssKey;
	}

	public void setThemeMainCssKey(String themeMainCssKey) {
		this.themeMainCssKey = themeMainCssKey;
	}

	public String getDefaultThemeRoot() {
		return defaultThemeRoot;
	}

	public void setDefaultThemeRoot(String defaultThemeRoot) {
		this.defaultThemeRoot = defaultThemeRoot;
	}

	public String getDefaultThemeType() {
		return defaultThemeType;
	}

	public void setDefaultThemeType(String defaultThemeType) {
		this.defaultThemeType = defaultThemeType;
	}

	public String getDefaultThemeStyle() {
		return defaultThemeStyle;
	}

	public void setDefaultThemeStyle(String defaultThemeStyle) {
		this.defaultThemeStyle = defaultThemeStyle;
	}

	public ThemeSkin getThemeSkin() {
		return themeSkin;
	}

	public void setThemeSkin(ThemeSkin themeSkin) {
		this.themeSkin = themeSkin;
	}

	@Override
	public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();
        
        defaultThemeRoot = getInitParameter(servletContext,ThemeSkin.THEME_ROOT_KEY, defaultThemeRoot);

    	defaultThemeType = getInitParameter(servletContext,ThemeSkin.THEME_TYPE_KEY, defaultThemeType);

    	defaultThemeStyle = getInitParameter(servletContext,ThemeSkin.THEME_STYLE_KEY, defaultThemeStyle) ;
        
        themeSkin = ThemeSkin.getInstance(servletContext.getRealPath("/"), defaultThemeRoot, defaultThemeType, defaultThemeStyle);
	}

	private String getInitParameter(ServletContext servletContext, String paramKey, String defaultThemeRoot2) {
		String result = defaultThemeRoot;
		
        String paramString = servletContext.getInitParameter(paramKey);
        if (paramString!=null && paramString.length()>0){
        	result = paramString;  
        }
        
		return result;
	}

	@Override
	public void sessionCreated(HttpSessionEvent se) {
        HttpSession session = se.getSession();

        if (themeService!=null){
        	// 配置了主题管理服务，使用主题管理服务提供的值
        	Map<String, String> result = themeService.getTheme();
        	String themeType = result.get(ThemeSkin.THEME_TYPE_KEY);
        	session.setAttribute(themeTypeKey,themeType);
        	String themeStyle = result.get(ThemeSkin.THEME_STYLE_KEY);
        	session.setAttribute(themeStyleKey,themeStyle);
        }else{
        	if (themeSkin!=null){
        		
        	}
        	// 未配置主题管理服务，使用默认配置或web环境配置
        	Object value = session.getAttribute(themeRootKey);
        	if (value==null||!(value instanceof ThemeSkin)){
        		session.setAttribute(themeRootKey, themeSkin);
        	}
        	
        	ThemeType themeType = null;
        	value = session.getAttribute(themeTypeKey);
        	if (value!=null){
        		if (value instanceof ThemeType){
        			themeType = (ThemeType)value;
        		}else if (value instanceof String){
        			if (themeSkin!=null){
            			themeType = themeSkin.getThemeType((String)value);
            			if (themeType==null){
            				themeType = themeSkin.getDefault();
            			}
        			}
        		}else{
        			if (themeSkin!=null){
        				themeType = themeSkin.getDefault();
        			}
        		}
        	}else{
        		if (themeSkin!=null){
    				themeType = themeSkin.getDefault();
        		}
        	}
        	session.setAttribute(themeTypeKey, themeType);
        	
            ThemeStyle themeStyle = null;
        	value = session.getAttribute(themeStyleKey);
        	if (value==null){
        		if(themeType!=null) {
                    themeStyle = themeType.getDefault();
        		}
        	}else{
        		if (value instanceof ThemeStyle){
                    themeStyle = (ThemeStyle)value;
        		}else if (value instanceof String){
                    themeStyle = themeType.getThemeStyle((String)value);
                    if (themeStyle==null){
                        themeStyle = themeType.getDefault();
                    }
        		}else{
                    themeStyle = themeType.getDefault();
        		}
        	}
        	session.setAttribute(themeStyleKey, themeStyle);
        	
        	log.debug("主题根目录="+session.getAttribute(themeRootKey)+" 主题类型="+session.getAttribute(themeTypeKey)+" 主题风格"+session.getAttribute(themeStyleKey)+" 主样式文件= "+ session.getAttribute(themeMainCssKey));
            
        	String mainCss=null;
        	value = session.getAttribute(themeMainCssKey);
        	if (value==null){
        		if(themeStyle!=null) {
            		mainCss = themeStyle.getMainCss();
        		}
        	}else{
        		if (value instanceof String){
            		mainCss = themeStyle.getMainCss();
        		}else{
            		mainCss = themeStyle.getMainCss();
        		}
        	}
        	session.setAttribute(themeMainCssKey, mainCss);
		}

    	log.debug("主题根目录="+session.getAttribute(themeRootKey)+" 主题类型="+session.getAttribute(themeTypeKey)+" 主题风格"+session.getAttribute(themeStyleKey)+" 主样式文件= "+ session.getAttribute(themeMainCssKey));
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		// TODO Auto-generated method stub
	
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		
	}

}
